package com.example.concurrentprogramming8016.disruptor.produceconsumer.singleproduceconsumer;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

/**
 * @author liyanhao
 * @Date 2023-01-08 11:44
 */
public class DisruptorDemo {
    public static void main(String[] args) {
        //创建disruptor
        Disruptor disruptor = new Disruptor<>(
                new OrderEventFactory(),
                1024 * 1024,
                Executors.defaultThreadFactory(),
                ProducerType.SINGLE,//单个消费者
                new YieldingWaitStrategy()//等待策略
        );
        //设置消费者用于处理ringbuffer的事件
        disruptor.handleEventsWith(new OrderEventHandler());
        disruptor.start();
        //创建ringbuffer容器
        RingBuffer ringBuffer = disruptor.getRingBuffer();
        OrderEventProducer orderEventProducer = new OrderEventProducer(ringBuffer);
        //发送消息
        for (int i = 0; i < 100; i++) {
            orderEventProducer.onData(i, "liyanhao:" + i);
        }

    }
}
